package acwing.堆;

import java.util.Scanner;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/11/17
 */
public class help {

    static int N = 100010;
    static int[] h = new int [N];
    static int size = 0;

    public static void down(int u) {

        int t = u;
        if (u * 2 <= size && h[u * 2] < h[t]) t = t * 2;
        if(u * 2 + 1 <= size && h[u*2+1]< h[t]) t = u*2+1;
        if (u != t) {
            swap(u, t);
            down(t);
        }
    }

    private static void swap(int i, int j) {

        int temp=h[i];
        h[i]=h[j];
        h[j]=temp;
    }

    public static void main(String[] args) {
        Scanner sy = new Scanner(System.in);
        int n = sy.nextInt();
        int m = sy.nextInt();
        for(int i = 1;i<=n;i++) h[i] = sy.nextInt();

        size = n;
        for(int i = n/2; i != 0; i--) down(i);

        while (m-- != 0) {
            System.out.print(h[1] + " ");
            h[1] = h[size--];

            down(1);
        }
    }
}
